package com.yidian.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yidian.controller.utils.BCrypt;
import com.yidian.entity.PageResult;
import com.yidian.entity.Result;
import com.yidian.pojo.system.Admin;
import com.yidian.server.system.AdminService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whb
 * @create 2021-06-08 16:36
 * @desciption 系统管理账户控制类
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Reference
    private AdminService adminService;

    @GetMapping("/findAll")
    public List<Admin> findAll() {
        return adminService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Admin> findPage(int page, int size) {
        return adminService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Admin> findList(@RequestBody Map<String, Object> searchMap) {
        return adminService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Admin> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return adminService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public Admin findById(Integer id) {
        return adminService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        adminService.add(admin);
        return new Result(0, "管理账户添加成功..");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return new Result(0, "管理账户修改成功..");
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        adminService.delete(id);
        return new Result(0, "管理账户删除成功..");
    }

    @PostMapping("/validateOldPwd")
    public Integer validateOldPwd(@RequestBody Map<String, Object> searchMap) {
        List<Admin> adminList = adminService.findList(searchMap);
        if (adminList.size() > 0) {
            Admin admin = adminList.get(0);
            String oldPwd = (String) searchMap.get("old");
            if (!BCrypt.checkpw(oldPwd, admin.getPassword())) {
                return 0;
            }
        } else {
            return 0;
        }
        return 1;
    }

    @PostMapping("/updatePassword")
    public Integer updatePassword(@RequestBody Map<String, Object> map) {
        Map<String, Object> searchMap = new HashMap<>();
        searchMap.put("loginName", map.get("loginName"));
        List<Admin> adminList = adminService.findList(searchMap);
        if (adminList.size() > 0) {
            try {
                Admin admin = adminList.get(0);
                admin.setPassword(BCrypt.hashpw((String) map.get("password"), BCrypt.gensalt()));
                adminService.update(admin);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        } else {
            return 0;
        }
        return 1;
    }
}
